/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iddscui;

import Main.Parameters;
import java.io.File;
import plugin.LibsThread;
import plugin.Plugin;
import plugin.PluginLoader;
import system.MessageOUTPUT;

/**
 *
 * @author ZeM
 */
public class iddsCUIrun implements Runnable {

    public String inp;

    public void run() {
        ConsoleInterface();
    }

    protected void ConsoleInterface() {
        //Starting the programm


        String[] input;



        try {



            input = inp.split(" ");
            {
                if (inp.equals("exit") | inp.equals("quit")) {//exit from program
                    Parameters.exit = true;
                } else if (input[0].equals("help")) { //start showing help
                    if (input.length == 1) {
                        //showing help
                        LibsThread lt = new LibsThread();
                        lt.lib_name = "iddsHELP";
                        new Thread(lt).start();
                    } else {//else error too much args
                        new MessageOUTPUT().OUTPUT("er0005", null);
                    }

                } else if (input[0].equals("key")) { //start showing help

                    if (input.length == 1) {
                        //showing help
                        LibsThread lt = new LibsThread();
                        lt.lib_name = "iddsKeyGenerator";
                        new Thread(lt).start();
                    } else {//else error too much args
                        new MessageOUTPUT().OUTPUT("er0005", null);
                    }

                } else if (input[0].equals("plugin")) {//plugin
                    if (input[1].equals("check")) {//checking plugin(s)
                        if (input[2].equals("all")) {//checking all plugins
                        } else {
                            for (int i = 2; i < input.length; i++) {
                                //check each plugin - input[i]

                                if (new Plugin().Check(input[i] + File.separatorChar + input[i] + ".jar")) {
                                    String[] mes = {input[i]};
                                    new MessageOUTPUT().OUTPUT("ev0006", mes);
                                }
                            }
                        }
                   
                    } else if (input[1].equals("about")) {
                        if (new Plugin().Exists(input[2])) {
                            //load plugin
                            boolean exists = false; //variable become true if plugin in loaded list has been found
                            int counter = 0; //counter for position in loaded list

                            int plugin_number = 0; //position of plugin;
                            do {
                                //if plugin found
                                if (Parameters.LoadedPluginNames[counter] != null) {
                                    if (Parameters.LoadedPluginNames[counter].equals(input[2])) {
                                        exists = true;
                                        plugin_number = counter;
                                       
                                    }
                                }
                                //if plugin not found
                                if (!exists) {
                                    //next position of counter;
                                    counter++;
                                }
                            } while (!exists & counter < Parameters.LoadedPluginNames.length);
                            //if plugin hasn't been found in loaded plugin list
                            if (!exists) {

                                boolean find = false; //variable become true when find free space to load plugin
                                int count = 0; //counter for position in loaded list
                                do {
                                    //if position in loaded plugin list is null
                                    if (Parameters.LoadedPluginNames[count] == null) {
                                        find = true;//find place to load


                                    } else {
                                        //add 1 to position
                                        count++;
                                    }
                                } while (!find & count < Parameters.LoadedPluginNames.length);

                                //load plugin

                                Parameters.LoadedPlugins[count] = new PluginLoader();
                                if (!Parameters.LoadedPlugins[count].Load(input[2])) {
                                    //cant load plugin


                                    String[] mes = {"" + input[2]};
                                    new MessageOUTPUT().OUTPUT("er0020", mes);

                                } else {
                                    Parameters.LoadedPluginNames[count] = input[2];
                                    plugin_number = count;
                                }

                            }

                            String message = Parameters.LoadedPlugins[plugin_number].About();
                            new MessageOUTPUT().OutputToCUI(message);
                            new MessageOUTPUT().OutputToGUI(message);

                          



                        }
                        //getting information about plugin inp[2]
                    } else {
                        new MessageOUTPUT().OUTPUT("er0005", null);
                    }


                } else if (input[0].equals("task")) {

                    if (input[1].equals("delete")) { //delete tasks
                        for (int i = 2; i < input.length; i++) {
                            if (new FileTask().Exist(Parameters.TaskFolder + input[i] + ".xml")) {
                                if (new FileTask().Delete(Parameters.TaskFolder + input[i] + ".xml")) {
                                    String[] mes = {input[i]};
                                    new MessageOUTPUT().OUTPUT("ev0009", mes);//
                                } else {
                                    //error "can't delete task"
                                    String[] mes = {input[i]};
                                    new MessageOUTPUT().OUTPUT("er0024", mes);//error
                                    }
                            } else {
                                //can't find file
                                String[] mes = {input[i]};
                                new MessageOUTPUT().OUTPUT("er0025", mes);//error
                            }
                        }
                    } else if (input[1].equals("run")) {
                    } else if (input[1].equals("list")) {
                        String[] list = new FileTask().List();
                        String[] mes = {"" + list.length};
                        new MessageOUTPUT().OUTPUT("ev0010", mes);
                        for (int i = 0; i < list.length; i++) {
                            new MessageOUTPUT().OutputToCUI(i + ") " + list[i]);
                            new MessageOUTPUT().OutputToGUI(i + ") " + list[i]);
                        }

                    } else if (input[1].equals("create")) {
                        LibsThread lt = new LibsThread();
                        lt.lib_name = "iddsTaskCreate";
                        new Thread(lt).start();
                    } else {
                        new MessageOUTPUT().OUTPUT("er0005", null);//error
                    }


                } else if (input[0].equals("gui")) {
                    //show GUI
                    LibsThread lt = new LibsThread();
                    lt.lib_name = "iddsGUI";
                    new Thread(lt).start();
                } else {
                    //show error about unsuported command
                    new MessageOUTPUT().OUTPUT("er0005", null);//error
                }
            }







        } catch (Exception IOex) {
        }



    }
}


